/*
 * @lc app=leetcode.cn id=14 lang=typescript
 *
 * [14] 最长公共前缀
 */

// @lc code=start
function longestCommonPrefix1(strs: string[]): string {
    const len=strs.length
    let com=""
    let firstStr = strs[0];
    let firstLen = firstStr.length;
    for( let cur = 0; cur < firstLen; cur++){
        let curChar = firstStr.charAt(cur);
        for(let i = 1; i < len; i++) {
            if(strs[i].charAt(cur)!==curChar){
                return com;
            }
        }
        com += curChar;
    }
    return com
};
// @lc code=end


// @lc code=start
function longestCommonPrefix2(strs: string[]): string {
    if(strs.length==0){
        return ""
    }
    let ans=strs[0]
    for(let i=0;i<strs.length;i++){
        let j=0
        for(;j<ans.length&&j<strs[i].length;j++){
            if(ans[j]!=strs[i][j]){
                break
            }
        }
        ans=ans.substr(0,j)
        if(ans=="") return ans
    }
    return ans
};
// @lc code=end




// @lc code=start
function longestCommonPrefix(strs: string[]): string {
    if(!strs||strs.length==0){
        return ""
    }
    let result=strs[0]
    for(let i=0;i<strs.length&&result;i++){
        let str=strs[i]
        while(str){
            if(result.startsWith(str)) break
            str=str.substring(0,str.length-1)
        }
        result=str
    }
    return result
};
// @lc code=end